require(['../js/require.conf.js'], function () {
  require(['jquery', 'OauthApi', 'OauthCache', 'WxJsdkSign', 'PageInit'], function ($, oauthApi, oauthCache, wxJsdk, pageInit) {
    console.log($().jquery);
    var $loadingToast = $('#loadingToast');
    pageInit.back(function () {
      closeWindow();
    })
    if (oauthCache.isLogin()) {
      handleLoginSuccess()
    } else {
      $loadingToast.fadeIn(100);
      oauthApi.doWxLogin({
        onSuccess: function (res) {
          oauthCache.saveOauth(res.data.oauth)
          oauthCache.saveUser(res.data.user)
          $loadingToast.fadeOut(100);
          handleLoginSuccess();
        },
        onError: function (errcode, errmsg, openid) {
          console.error("==登录失败======>", errcode, errmsg, openid)
          $loadingToast.fadeOut(100);
          //1.未注册
          if (errcode == CODE_UNREGISTER) {
            //跳转到注册页
          } else {
            //2.其他
          }
        }
      });
    }


    function handleLoginSuccess() {
      let role = oauthCache.getMainRole();
      console.log("当前用户角色:", role)
      //todo 跳转到主页
    };

    function closeWindow() {
      wxJsdk.wxJsSdkSignature(window.location.href, ['closeWindow'], function (success, wx) {
        if (success)
          wx.closeWindow();
      })
    }
  });
})